package com.goparry.taskman.browse;

import java.util.List;

import com.goparry.taskman.R;
import com.goparry.taskman.repository.RObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;

/**
 * @author davidp
 * DocumentAdapter to process items in the Document List.
 */

public class DocumentAdapter extends ArrayAdapter<RObject> {
	
	int resource;

	public DocumentAdapter(Context context, int _resource, int textViewResourceId,
			List<RObject> objects) {
		super(context, _resource, textViewResourceId, objects);
		
		resource = _resource; //This is a pointer to the layout that displays a task
	}

	//@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout newView;
		RObject document;
		
		// Get the specific document
		document = (RObject)this.getItem(position);
		newView = new LinearLayout(getContext());
		
		// Inflate a new view if this is not an update.
		if (convertView == null) {
			newView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(resource, newView, true);
		} else {
			newView = (LinearLayout)convertView;
		}
		
		if (document.getIsContainer()) {
			ImageView type = (ImageView)newView.findViewById(R.id.icon);
//			if ("vault".equals(document.getFormat())) {
//				type.setImageResource(R.drawable.vault);
//			} else {
//				type.setImageResource(R.drawable.folder_yellow);
//			}
			
			if (document.getFormat().equalsIgnoreCase("0")) {
				if ("assets".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_asset);
				} else if ("projects".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_project);
				} else {
					type.setImageResource(R.drawable.ic_folder_generic);
				}				
			} else if (document.getFormat().equalsIgnoreCase("1")) {
				type.setImageResource(R.drawable.ic_folder_asset);
			} else if (document.getFormat().equalsIgnoreCase("2")) {
				type.setImageResource(R.drawable.ic_folder_project);
			} else if (document.getFormat().equalsIgnoreCase("3")) {
				if ("sops".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_sops);
				} else if ("architectural".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_architectural);
				} else if ("electrical".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_electrical);
				} else if ("mechanical".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_mechanical);
				} else if ("datasheets".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_datasheets);
				} else if ("drawings".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_drawings);
				} else if ("manuals".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_manuals);
				} else if ("contracts".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_contracts);
				} else if ("documents".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_documents);
				} else if ("standards".equalsIgnoreCase(document.getName())) {
					type.setImageResource(R.drawable.ic_folder_standards);
				} else {
					type.setImageResource(R.drawable.ic_folder_generic);
				}				
			} else {
				type.setImageResource(R.drawable.ic_folder_generic);
			}
			
			TextView name = (TextView)newView.findViewById(R.id.name);
			name.setText(document.getName());

			name = (TextView)newView.findViewById(R.id.title);
			name.setText(document.getTitle());
			
			name = (TextView)newView.findViewById(R.id.revision);
			name.setText("");
			
			name = (TextView)newView.findViewById(R.id.status);
			name.setText("");		

			
		}	else {
		
			ImageView type = (ImageView)newView.findViewById(R.id.icon);
			if ("pdf".equals(document.getFormat())) {
				type.setImageResource(R.drawable.pdf_icon);
			} else if ("doc".equals(document.getFormat())) {
				type.setImageResource(R.drawable.ic_doc_msw);
			} else if ("xls".equals(document.getFormat())) {
				type.setImageResource(R.drawable.ic_doc_excel);
			} else if ("ppt".equals(document.getFormat())) {
					type.setImageResource(R.drawable.ic_doc_ppt);
			} else if ("dwg".equals(document.getFormat())) {
					type.setImageResource(R.drawable.ic_doc_drawing);
			} else {
					type.setImageResource(R.drawable.ic_doc_generic);
			}
	
			
			TextView name = (TextView)newView.findViewById(R.id.name);
			name.setText(document.getName());
			
			name = (TextView)newView.findViewById(R.id.title);
			name.setText(document.getTitle());
			
			name = (TextView)newView.findViewById(R.id.revision);
			name.setText("Rev " + document.getRevision());
			
			name = (TextView)newView.findViewById(R.id.status);
			name.setText(document.getStatus());		
		}
		return newView;
	}
}
